#include "base/ClosedCylinder.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform2.hpp>
#include <glm/gtx/projection.hpp>

ClosedCylinder::ClosedCylinder(GLuint programHandler, GLShaderConfig shaderConfig, float radium, float height) :
	GLStructure(programHandler, shaderConfig),
	radium(radium),
	height(height) {
	top = new Circle(programHandler, shaderConfig);
	bottom = new Circle(programHandler, shaderConfig);
	cylinder = new Cylinder(programHandler, shaderConfig);
}

ClosedCylinder::~ClosedCylinder() {
	delete top;
	delete bottom;
	delete cylinder;
}

void ClosedCylinder::updateComponents() {
	glm::mat4 transformation = glm::scale(transformationMatrix, glm::vec3(radium, radium, height));
	cylinder->transform(transformation);

	transformation = glm::translate(transformationMatrix, glm::vec3(0, 0, height / 2));
	transformation = glm::scale(transformation, glm::vec3(radium, radium, 1));
	top->transform(transformation);


	transformation = glm::rotate(transformationMatrix, 180.0f, glm::vec3(1, 0, 0));
	transformation = glm::translate(transformation, glm::vec3(0, 0, height / 2));
	transformation = glm::scale(transformation, glm::vec3(radium, radium, 1));
	bottom->transform(transformation);
}

void ClosedCylinder::update (glm::mat4 viewMatrix, glm::mat4 modelMatrix) {
	updateComponents();
	top->update(viewMatrix, modelMatrix);
	bottom->update(viewMatrix, modelMatrix);
	cylinder->update(viewMatrix, modelMatrix);
}

void ClosedCylinder::setColor(float r, float g, float b) {
	top->setColor(r, g, b);
	bottom->setColor(r, g, b);
	cylinder->setColor(r, g, b);
}

void ClosedCylinder::setTextureData(float diffuseTexture[], float dirtTexture[]) {
	top->setTextureData(diffuseTexture, dirtTexture);
	bottom->setTextureData(diffuseTexture, dirtTexture);
	cylinder->setTextureData(diffuseTexture, dirtTexture);
}
